package com.yibo.support.handler;

import com.yibo.support.exception.YBException;
import com.yibo.support.pojo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@ControllerAdvice
@RestControllerAdvice
public class YBErrorHandler   {

    @ExceptionHandler(RuntimeException.class)
    @SuppressWarnings("unchecked")
    public Result handler(Exception e){
        if(e instanceof YBException){
            return new Result(((YBException) e).getCode(),e.getMessage(),null);
        }

        return new Result(-1,e.getMessage(),null);
    }
}
